﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VanTaiBLL;
using VanTaiData;
using VanTaiProperty;
using Roles;
using System.Web.UI.HtmlControls;

namespace VanTaiTMS.SubPage
{
    public partial class uc_khach_hang : System.Web.UI.UserControl
    {
        #region[Define]
        private DataTable dtCustomer;
        private DataTable dtProvince;
        private DataTable dtDepartment;
        private DataTable dtTypesOfBusinesses;
        private DataTable dtRepairMaterial;
        protected string SuggestionList;
        protected string StrAlert = "";
        private clsTMProvinceBusiness clsProvince = new clsTMProvinceBusiness();
        private clsTMDistrictBusiness clsDistric = new clsTMDistrictBusiness();
        private clsTMDepartmentBusiness clsDepartment = new clsTMDepartmentBusiness();
        private clsTMTypesOfBusinessesBusiness clsTypesOfBusinesses = new clsTMTypesOfBusinessesBusiness();
        private clsTMSymbolRepairMaterialsBusiness clsRepairMaterial = new clsTMSymbolRepairMaterialsBusiness();
        private clsTMCustomersBusiness clsCustommer = new clsTMCustomersBusiness();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            PanelChonSoDienThoai.Visible = false;
            txtSoDienThoai10.Visible = false;
            txtSoDienThoai11.Visible = false;
            if (!IsPostBack)
            {
                BindGridDrQuanHuyen();
                BindGridProvince();
                BindGridDeparment();
                BindGridTypesOfBusinesses();
                BindGridRepairMaterial();
            }
        }
        
        //---------------button-popup---------------------
        protected void imgrefesh_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtMaKhachHang.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào mã khách hàng !";
                OpenDialog("#ucthongbaokhachang");
                updatePanelThongBao.Update();
            }
            if (txtTenKhachHang.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào tên khách hàng !";
                OpenDialog("#ucthongbaokhachang");
                updatePanelThongBao.Update();
            }
            if (txtNgaySinh.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào ngày sinh nhân viên !";
                OpenDialog("#ucthongbaokhachang");
                updatePanelThongBao.Update();
            }

            if (drChonSoDienThoai.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn số điện thoại !";
                OpenDialog("#ucthongbaokhachang");
                updatePanelThongBao.Update();
            }

            if (txtSoDienThoai10.Text == "" || txtSoDienThoai11.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào số điện thoại nhân viên !";
                OpenDialog("#ucthongbaokhachang");
                updatePanelThongBao.Update();
            }
            if (txtNGayKy.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào ngày ký !";
                OpenDialog("#ucthongbaokhachang");
                updatePanelThongBao.Update();
            }
            if (txtNgayThanhLy.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào ngày thanh lý !";
                OpenDialog("#ucthongbaokhachang");
                updatePanelThongBao.Update();
            }
            if (drTinhThanh.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn tĩnh / thành !";
                OpenDialog("#ucthongbaokhachang");
                updatePanelThongBao.Update();
            }
            if (drQuanHuyen.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn Quận/Huyện !";
                OpenDialog("#ucthongbaokhachang");
                updatePanelThongBao.Update();
            }
            if (drBoPhan.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn bộ phận !";
                OpenDialog("#ucthongbaokhachang");
                updatePanelThongBao.Update();
            }
            if (drLoaiDaiLy.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn loại đại lý/DN !";
                OpenDialog("#ucthongbaokhachang");
                updatePanelThongBao.Update();
            }
            if (drLinhVuc.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn lĩnh vực !";
                OpenDialog("#ucthongbaokhachang");
                updatePanelThongBao.Update();
            }

            if (txtMaKhachHang.Text != "" && txtTenKhachHang.Text != ""
                && txtNgaySinh.Text != "" && txtNGayKy.Text != ""
                && txtNgayThanhLy.Text != ""
                && drTinhThanh.SelectedValue.ToString() != "-1"
                && drQuanHuyen.SelectedValue.ToString() != "-1"
                && drBoPhan.SelectedValue.ToString() != "-1"
                && drLoaiDaiLy.SelectedValue.ToString() != "-1"
                && drLinhVuc.SelectedValue.ToString() != "-1"
                && drChonSoDienThoai.SelectedValue.ToString() != "-1"
                && txtSoDienThoai10.Text != "" || txtSoDienThoai11.Text != ""
                && txtDiaChi.Text != "")
            {
                InsertCustomer();
            }
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#ucthongbaokhachang");
        }
       
        protected void drChonSoDienThoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drChonSoDienThoai.SelectedValue.ToString() == "1")
            {
                PanelChonSoDienThoai.Visible = true;
                txtSoDienThoai10.Visible = true;
                txtSoDienThoai11.Visible = false;
            }
            if (drChonSoDienThoai.SelectedValue.ToString() == "2")
            {
                PanelChonSoDienThoai.Visible = true;
                txtSoDienThoai10.Visible = false;
                txtSoDienThoai11.Visible = true;
            }
            if (drChonSoDienThoai.SelectedValue.ToString() == "-1")
            {
                PanelChonSoDienThoai.Visible = false;
                txtSoDienThoai10.Visible = false;
                txtSoDienThoai11.Visible = false;
                txtSoDienThoai10.Text = "";
                txtSoDienThoai11.Text = "";
            }
        }
        protected void drTinhThanh_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridDrQuanHuyen();
        }
        //------------Thêm-Nhanh-----------------
        protected void linkThemNhanhTinhThanh_Click(object sender, EventArgs e)
        {
            OpenDialog("#themnhanhtinhthanh");
        }
        protected void linkThemNhanhQuanHuyen_Click(object sender, EventArgs e)
        {
            OpenDialog("#themnhanhquanhuyen");
        }
        protected void linkThemNhanhBoPhan_Click(object sender, EventArgs e)
        {
            OpenDialog("#themnhanhbophan");
        }
        protected void linkThemNhanhLoaiDaiLy_Click(object sender, EventArgs e)
        {
            OpenDialog("#themnhanhloaidailydoanhnghiep");
        }
        protected void linkThemNhanhLinhVuc_Click(object sender, EventArgs e)
        {
            OpenDialog("#themnhanhlinhvuc");
        }
        #endregion

        #region[Function]
        protected void BindGridProvince()
        {
            dtProvince = clsProvince.BindGirdTMProvince("");
            if (dtProvince != null && dtProvince.Rows.Count > 0)
            {
                drTinhThanh.DataSource = dtProvince;
                drTinhThanh.DataTextField = "TenTinh";
                drTinhThanh.DataValueField = "STT";
                drTinhThanh.DataBind();
            }
        }
        protected void BindGridDrQuanHuyen()
        {
            drQuanHuyen.Items.Clear();
            drQuanHuyen.Items.Add(new ListItem("--Vui lòng chọn quận huyện--", "-1"));
            drQuanHuyen.DataSource = clsDistric.BindGirdTMDistrictDetail(int.Parse(drTinhThanh.SelectedValue.ToString()));
            drQuanHuyen.DataTextField = "TenQuanHuyen";
            drQuanHuyen.DataValueField = "STT";

        }
        protected void BindGridDeparment()
        {
            dtDepartment = clsDepartment.BindGirdTMDepartment();
            if (dtDepartment != null && dtDepartment.Rows.Count > 0)
            {
                drBoPhan.DataSource = dtDepartment;
                drBoPhan.DataTextField = "Department_Name";
                drBoPhan.DataValueField = "Department_ID";
                drBoPhan.DataBind();
            }
        }
        protected void BindGridTypesOfBusinesses()
        {
            dtTypesOfBusinesses = clsTypesOfBusinesses.BindGirdTMTypesOfBusinesses();
            if (dtTypesOfBusinesses != null && dtTypesOfBusinesses.Rows.Count > 0)
            {
                drLoaiDaiLy.DataSource = dtTypesOfBusinesses;
                drLoaiDaiLy.DataTextField = "TypesOfBusinesses_Name";
                drLoaiDaiLy.DataValueField = "TypesOfBusinesses_ID";
                drLoaiDaiLy.DataBind();
            }
        }
        protected void BindGridRepairMaterial()
        {
            dtRepairMaterial = clsRepairMaterial.BindGirdTMSymbolRepairMaterials();
            if (dtRepairMaterial != null && dtRepairMaterial.Rows.Count > 0)
            {
                drLinhVuc.DataSource = dtRepairMaterial;
                drLinhVuc.DataTextField = "SymbolRepairMaterials_Name";
                drLinhVuc.DataValueField = "SymbolRepairMaterials_ID";
                drLinhVuc.DataBind();
            }
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            dtCustomer = clsCustommer.BindGirdTMCustomers();
            if (dtCustomer != null && dtCustomer.Rows.Count > 0)
            {
                foreach (DataRow dr in dtCustomer.Rows)
                {
                    if (txtMaKhachHang.Text == dr["Customers_Alias"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertCustomer()
        {
            try
            {
                if (CheckInsert())
                {
                    lbThongBaoLoi.Text = "Mã khách hàng này đã tồn tại !";
                    OpenDialog("#ucthongbaokhachang");
                    updatePanelThongBao.Update();
                }
                else
                {
                    VanTaiData.clsTMCustomersProperty obj = new clsTMCustomersProperty();
                    obj.Customers_Alias = txtMaKhachHang.Text;
                    obj.Customers_Name = txtTenKhachHang.Text;
                    obj.Customers_Brithday = DateTime.Parse(txtNgaySinh.Text);
                    obj.Customers_Tax_Code = txtMaSoThue.Text;
                    obj.Customers_Dealer_ID = txtMaDaiLy.Text;
                    obj.Customers_Minutes_Number = txtSoHopDong.Text;
                    obj.Customers_General_Name = txtTenGiamDocTruongPhong.Text;
                    obj.Customers_Day_Signed = DateTime.Parse(txtNGayKy.Text);
                    obj.Customers_Day_Liquidation = DateTime.Parse(txtNgayThanhLy.Text);
                    obj.Customers_Province_ID = int.Parse(drTinhThanh.SelectedValue.ToString());
                    obj.Customers_Districts_ID = int.Parse(drQuanHuyen.SelectedValue.ToString());
                    obj.Customers_Division_ID = int.Parse(drBoPhan.SelectedValue.ToString());
                    obj.Customers_Type_DN_ID = int.Parse(drLoaiDaiLy.SelectedValue.ToString());
                    obj.Customers_Alternative_Materials_ID = int.Parse(drLinhVuc.SelectedValue.ToString());
                    obj.Customers_Person_Responsible = txtNganHang.Text;
                    obj.Customers_Address = txtDiaChi.Text;
                    obj.Customers_Note = txtGhiChu.Text;
                    #region
                    if (drChonSoDienThoai.SelectedValue == "1")
                    {
                        PanelChonSoDienThoai.Visible = true;
                        txtSoDienThoai10.Visible = true;
                        txtSoDienThoai11.Visible = false;
                        obj.Customers_Phone_Number = txtSoDienThoai10.Text;
                    }
                    if (drChonSoDienThoai.SelectedValue == "2")
                    {
                        PanelChonSoDienThoai.Visible = true;
                        txtSoDienThoai10.Visible = false;
                        txtSoDienThoai11.Visible = true;
                        obj.Customers_Phone_Number = txtSoDienThoai11.Text;
                    }
                    #endregion
                    obj.Customers_Fax = txtSoFax.Text;
                    obj.Customers_Email = txtEmail.Text;
                    obj.Customers_Account_Code = txtSoTaiKhoan.Text;
                    //------------------
                    obj.Customers_Name_C = DBNull.Value.ToString();
                    obj.Customers_Name_E = DBNull.Value.ToString();
                    obj.Customers_Trade_Names = DBNull.Value.ToString();
                    obj.Customers_CMT = DBNull.Value.ToString();
                    obj.Customers_Dational_ID = 0;
                    obj.Customers_Domain_ID = 0;
                    obj.Customers_Status = 0;
                    obj.Customers_Degree = 0;
                    obj.Customers_Subjective_Units = DBNull.Value.ToString();
                    obj.Customers_Supply_ID = 0;
                    obj.Customers_Person_Responsible = DBNull.Value.ToString();
                    obj.Customers_Status_Review = false;
                    obj.Customers_Branch_ID = 2;
                    VanTaiBLL.clsTMCustomersBusiness.InsertTMCustomers(obj);
                    lbThongBaoLoi.Text = "Thêm mẫu tin thành công !";
                    OpenDialog("#ucthongbaokhachang");
                    updatePanelThongBao.Update();
                    ClearTextBox();
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#ucthongbaokhachang");
                updatePanelThongBao.Update();
            }
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion

        #region[Clear-TextBox]
        protected void ClearTextBox()
        {
            txtMaKhachHang.Text = "";
            txtTenKhachHang.Text = "";
            txtNgaySinh.Text = "";
            txtMaSoThue.Text = "";
            txtMaDaiLy.Text = "";
            txtSoHopDong.Text = "";
            txtTenGiamDocTruongPhong.Text = "";
            txtNGayKy.Text = "";
            txtNgayThanhLy.Text = "";
            drTinhThanh.SelectedIndex = -1;
            drQuanHuyen.SelectedIndex = -1;
            drBoPhan.SelectedIndex = -1;
            drLoaiDaiLy.SelectedIndex = -1;
            drLinhVuc.SelectedIndex = -1;
            txtSoDienThoai10.Text = "";
            txtSoDienThoai11.Text = "";
            txtSoFax.Text = "";
            txtEmail.Text = "";
            txtSoTaiKhoan.Text = "";
            txtNganHang.Text = "";
            txtDiaChi.Text = "";
            txtGhiChu.Text = "";
        }
        #endregion
    }
}